
package paper.image;



import java.awt.image.*;
import java.io.*;
import java.awt.geom.*;
import java.awt.Rectangle;
////import java.awt.font.TextLayout;
import java.util.Enumeration;
import java.util.Vector;

import paper.image.ImageSegment;
import paper.image.ImageHold;
import paper.image.ImageConstants;

/**
 *  attempts to identify segments. 
 */

public class SegmentID 
{

	Vector imagesegments;		
	BufferedImage image;
	Raster blurRaster;
	ImageHold imageHold;
	int leftMargin;
	int rightMargin;
	int width;

	public SegmentID (Vector segments,int leftMargin, int rightMargin,int width){
		this.leftMargin=leftMargin;
		this.rightMargin=rightMargin;
	  	this.width=width;
		imagesegments=new Vector();
		boolean foundTitle=false;
		for(Enumeration e=segments.elements(); e.hasMoreElements();){
			Rectangle tmpRect=(Rectangle) e.nextElement();
			ImageSegment tmp=new ImageSegment(tmpRect);
			// consider a size limit for image segments!
			tmp.setDensity(ImageHold.getDensity(tmpRect));
			tmp.setDescriptiveLocation(getAlignment(tmpRect));
			tmp.setIdentity(getSimpleIdentity(tmp));
			// get font height(s) of imagesegment 
			// try the 'title is in centre' heuristic
			/*
			if(!foundTitle){
				if(tmp.getDescriptiveLocation()==ImageConstants.ALIGN_FULLWIDTH || tmp.getDescriptiveLocation()==ImageConstants.ALIGN_CENTER){
					if(tmp.getIdentity()==ImageConstants.IS_UNKNOWN){
						System.out.println("Found central title");
						tmp.setIdentity(ImageConstants.IS_TITLE);
						foundTitle=true;
					}
				}
			}*/
			imagesegments.addElement(tmp);
		}
		// estimate densities
		
	} 

	public SegmentID (Vector segments,ImageHold imageHold){
		this.imageHold=imageHold;
		this.leftMargin=imageHold.getLeftMargin();
		this.rightMargin=imageHold.getRightMargin();
		imagesegments=new Vector();
		boolean foundTitle=false;
		for(Enumeration e=segments.elements(); e.hasMoreElements();){
			Rectangle tmpRect=(Rectangle) e.nextElement();
			ImageSegment tmp=new ImageSegment(tmpRect);
			tmp.setDensity(imageHold.getDensity(tmpRect));
			tmp.setDescriptiveLocation(getAlignment(tmpRect));
			tmp.setIdentity(getSimpleIdentity(tmp));
			imagesegments.addElement(tmp);
		}
		// estimate densities
		
	} 
	public Rectangle getProbableTitle(){
		
		// this doesn't work. So, how do we do this?

		for(Enumeration e=imagesegments.elements(); e.hasMoreElements();){
			ImageSegment tmp= (ImageSegment) e.nextElement();
				
			if(tmp.getIdentity()==ImageConstants.IS_TITLE){
				System.out.println("Found title");
				return tmp.getRectangle();
			}
		}
		// uh oh
		for (Enumeration e2=imagesegments.elements();e2.hasMoreElements();){
			ImageSegment tmp2=(ImageSegment) e2.nextElement();
			if(tmp2.getIdentity()!=ImageConstants.IS_LINE && tmp2.getDescriptiveLocation()!=ImageConstants.ALIGN_RIGHT){
				return tmp2.getRectangle(); // return the first
			}
		}
		return new Rectangle(0,0,0,0);

	}

	public int getSimpleIdentity(ImageSegment tmp){
		// replace this with a USEFUL test!
		if (tmp.getHeight()<10){
			if(tmp.getWidth()<20){
				System.out.println("Is detail");
				return ImageConstants.IS_DETAIL;
			} else{
				System.out.println("Is line ");
				return ImageConstants.IS_LINE;	

			}
		}	
		System.out.println("Is unknown");
		return ImageConstants.IS_UNKNOWN;
		
	}

	public int getAlignment(Rectangle tmpRect){
		// get distance from left margin. Get distance from right margin. If similar (not necessarily equal) is centered.
		// if one markedly long than the other, is either right or left
		int deltaL=(int)tmpRect.getX()-leftMargin;
		int rightposn=(int)tmpRect.getX()+(int)tmpRect.getWidth();
		int deltaR=rightMargin-rightposn;
		if(-10<deltaL && deltaL<10){ // fudge factor
			if(-10<deltaR && deltaR<10){
				System.out.println("Fullwidth");
				return ImageConstants.ALIGN_FULLWIDTH;	
			} else {
				System.out.println("Left");
				return ImageConstants.ALIGN_LEFT;
			}		
		}
		if(-10<deltaR&&deltaR<10){
			System.out.println("Right");
			return ImageConstants.ALIGN_RIGHT; // checked for fullwidth
		}	
		if((deltaL-deltaR)>-10 && (deltaL-deltaR)<10) {
			System.out.println("Center");
			return ImageConstants.ALIGN_CENTER;
		}
		int pagedL=(int)tmpRect.getX();
		int pagedR=this.width-(int)(tmpRect.getX()+tmpRect.getWidth());
		if(-6<(pagedR-pagedL) && (pagedR-pagedL)<6){
			System.out.println("Absolute center");
			return ImageConstants.ALIGN_EDGECENTER;
		}
		System.out.println("Unknown");
		return ImageConstants.ALIGN_UNKNOWN;
	}

}

/* Modeline for ViM {{{
 * vim: set ts=4:
 * vim600: fdm=marker fdl=0 fdc=3:
 * }}} */

